package com.ruoyi.yyq.controller;

import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.yyq.domain.CarCoupon;
import com.ruoyi.yyq.mapper.CarCouponMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.yyq.domain.CarUserCoupon;
import com.ruoyi.yyq.service.ICarUserCouponService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户优惠卷中间Controller
 * 
 * @author ruoyi
 * @date 2025-05-12
 */
@RestController
@RequestMapping("/usercoupon")
@CrossOrigin
public class CarUserCouponController extends BaseController
{
    @Autowired
    private ICarUserCouponService carUserCouponService;
    @Autowired
    private CarCouponMapper carCouponMapper;

    /**
     * 查询用户优惠卷中间列表
     */
    @RequiresPermissions("yyq:usercoupon:list")
    @GetMapping("/list")
    public TableDataInfo list(CarUserCoupon carUserCoupon)
    {
        startPage();
        List<CarUserCoupon> list = carUserCouponService.selectCarUserCouponList(carUserCoupon);
        return getDataTable(list);
    }

    /**
     * 导出用户优惠卷中间列表
     */
    @RequiresPermissions("yyq:usercoupon:export")
    @Log(title = "用户优惠卷中间", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CarUserCoupon carUserCoupon)
    {
        List<CarUserCoupon> list = carUserCouponService.selectCarUserCouponList(carUserCoupon);
        ExcelUtil<CarUserCoupon> util = new ExcelUtil<CarUserCoupon>(CarUserCoupon.class);
        util.exportExcel(response, list, "用户优惠卷中间数据");
    }

    /**
     *
     * 暴露根据用户id和优惠卷id查询用户优惠卷中间信息
     * @param uid
     * @param cid
     * @return
     */
    @InnerAuth
    @GetMapping(value = "/user/{uid}/coupon/{cid}")
    public AjaxResult getInfo(@PathVariable("uid") Long uid,@PathVariable("cid") Long cid){
        //根据用户uid查询用户信息，判断是否是新用户（一个月）
        //如果是新用户，增添数据，否则返回不符和条件
        return success();
    }
    /**
     *
     * 暴露根据用户id查询用户优惠卷
     */
    @RequiresPermissions("yyq:usercoupon:query")
//    @InnerAuth
    @GetMapping(value = "/getquan/{uid}")
    public AjaxResult getquan(@PathVariable("uid") Long uid){
        List<CarUserCoupon> carUserCoupons = carUserCouponService.selectCarUserCouponByUid1(uid);
        List<Long> cidList = carUserCouponService.selectCarUserCouponByUid(uid);
//        System.out.println("获取成功-----cidlist"+cidList.toString());
        if (cidList.isEmpty()){
            return error("该用户没有优惠券");
        }else {
            List<CarCoupon> coupons = carCouponMapper.selectCarCouponListByIds(cidList);
//            System.out.println("获取成功-----");
            return success(coupons);
        }
    }
    @RequiresPermissions("yyq:usercoupon:query")
//    @InnerAuth
    @GetMapping(value = "/getquan1/{uid}")
    public AjaxResult getquan1(@PathVariable("uid") Long uid){
        List<CarUserCoupon> carUserCoupons = carUserCouponService.selectCarUserCouponByUid1(uid);
        return success(carUserCoupons);
    }

//    @RequiresPermissions("yyq:usercoupon:query")
    @InnerAuth
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(carUserCouponService.selectCarUserCouponById(id));
    }

    /**
     * 新增用户优惠卷中间
     */
    @RequiresPermissions("yyq:usercoupon:add")
    @Log(title = "用户优惠卷中间", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarUserCoupon carUserCoupon)
    {
        System.out.println("接收新增用户优惠卷中间表数据"+carUserCoupon.toString());
        return toAjax(carUserCouponService.insertCarUserCoupon(carUserCoupon));
    }

    /**
     * 修改用户优惠卷中间
     */
    @RequiresPermissions("yyq:usercoupon:edit")
    @Log(title = "用户优惠卷中间", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CarUserCoupon carUserCoupon)
    {
        return toAjax(carUserCouponService.updateCarUserCoupon(carUserCoupon));
    }

    /**
     * 删除用户优惠卷中间
     */
    @RequiresPermissions("yyq:usercoupon:remove")
    @Log(title = "用户优惠卷中间", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(carUserCouponService.deleteCarUserCouponByIds(ids));
    }
}
